package com.qwc.blog.web.admin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.qwc.blog.common.annotation.AdminController;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.config.FileProperties;
import com.qwc.blog.entity.Article;
import com.qwc.blog.entity.SysPicture;
import com.qwc.blog.service.IArticleService;
import com.qwc.blog.service.ISysPictureService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;
import java.io.FileFilter;
import java.sql.Struct;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author qiuwencong
 * @Date 2022/3/4 20:49
 * @Package com.qwc.blog.web.admin
 * @Description
 */
@Slf4j
@AdminController("common")
public class CommonController {

    @Autowired
    private FileProperties properties;

    @Autowired
    private ISysPictureService sysPictureService;

    @Autowired
    private IArticleService articleService;

    @GetMapping("clear")
    public ResponseEntity<Object> clear() {
        List<SysPicture> pictures = sysPictureService.list();
        List<String> collect = pictures.stream().map(SysPicture::getImageUrl).collect(Collectors.toList());
        List<Article> articles = articleService.list();
        List<String> collect1 = articles.stream().map(Article::getThumbnail).collect(Collectors.toList());
        collect.addAll(collect1);
        List<String> reserve = collect.stream().filter(Objects::nonNull)
                .map(url -> getName(url))
                .collect(Collectors.toList());
        ClearFileFilter clearFileFilter = new ClearFileFilter();
        clearFileFilter.setUrls(reserve);
        File sys = new File(properties.getProfile() + properties.getSys());
        File cover = new File(properties.getProfile() + properties.getCover());
        List<File> files1 = FileUtil.loopFiles(sys,clearFileFilter);
        List<File> files2 = FileUtil.loopFiles(cover,clearFileFilter);
        files1.addAll(files2);
        for (File file : files1) {
            if (file.exists()) {
                file.delete();
            }
        }
        FileUtil.cleanEmpty(sys);
        FileUtil.cleanEmpty(cover);
        return ResponseEntity.ok(true);
    }

    private String getName(String url){
        String sub = StrUtil.sub(url, url.lastIndexOf("\\") + 1, url.length());
        return StrUtil.sub(sub, sub.lastIndexOf("/") + 1, sub.length());
    }

    public static void main(String[] args) {
        List<File> sys = FileUtil.loopFiles("D:/devData/myWorkspace/sys");
        List<File> cover = FileUtil.loopFiles("D:/devData/myWorkspace/cover");
        for (File sy : sys) {
            System.out.println(sy.getName());
        }for (File sy : cover) {
            System.out.println(sy.getName());
        }

    }

    @Data
    class ClearFileFilter implements FileFilter{

        private List<String> urls;

        @Override
        public boolean accept(File pathname) {
            String absolutePath = pathname.getName();
            for (String url : urls) {
                if (absolutePath.equals(url)) {
                    return false;
                }
            }
            return true;
        }
    }
}
